package py.com.fms.com.persistence.services;

import java.sql.SQLException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import com.ibatis.sqlmap.client.SqlMapClient;

import py.com.fms.com.bean.PaymentOrderHeadBean;
import py.com.fms.com.bean.PaymentOrderDetBean;
import py.com.fms.com.bean.PurchasesHeadBean;
import py.com.fms.exception.CommonException;
import py.com.fms.fms.bean.PersonBean;
import py.com.fms.persistence.services.FmsServices;

@SuppressWarnings("unused")
public class PaymentOrderServices extends FmsServices {

	public PaymentOrderServices(SqlMapClient client) {
		super(client);
		// TODO Auto-generated constructor stub
	}
	
	/**
	 [[[
	 [[ The following method retrieve a list of all records
	 [[ Normally used to load a grid.
	 */
	
	/**
	 * Returns an List object with all data of the Entity Header. 
	 *
	 * @return      a list with all data
	 */
	@SuppressWarnings("unchecked")
	public List<PaymentOrderHeadBean> getAllPaymentOrderHead() throws CommonException{
		List<PaymentOrderHeadBean> queryForList;
		try {
			
			//[[ queryForList is an implemented interface of iBatis]] 
			
			queryForList = (List<PaymentOrderHeadBean>)this.getConfig().queryForList("paymentOrder.getAllPaymentOrderHead");
			return queryForList;
			
		} catch (SQLException e) {
			throw new CommonException(e);
		}
	}
	
	/**
	 * Returns an List of PaymentOrders for a specify supplier. 
	 * The argument must be a Long Object with the value of the Supplier Id
	 *
	 * @param  		the supplier id
	 * @return      a list with the purchases for the passed argument (Supplier Id)
	 */
	@SuppressWarnings("unchecked")
	public List<PaymentOrderHeadBean> getPaymentOrderBySupplierId(Long supplierId) throws CommonException{
		List<PaymentOrderHeadBean> queryForList;
		try {
			
			//[[ queryForList is an implemented interface of iBatis]] 
			
			queryForList = (List<PaymentOrderHeadBean>)this.getConfig().queryForList("paymentOrder.getPaymentOrderBySupplierId", supplierId);
			return queryForList;
			
		} catch (SQLException e) {
			throw new CommonException(e);
		}
	}
	
	
	/**
	 * Returns an List object with all data of the Entity Detail. 
	 *
	 * @return      a list with all data
	 */
	@SuppressWarnings("unchecked")
	public List<PaymentOrderDetBean> getAllPaymentOrderDet() throws CommonException{
		List<PaymentOrderDetBean> queryForList;
		try {
			
			//[[ queryForList is an implemented interface of iBatis]] 
			
			queryForList = (List<PaymentOrderDetBean>)this.getConfig().queryForList("paymentOrder.getAllPaymentOrderDet");
			return queryForList;
			
		} catch (SQLException e) {
			throw new CommonException(e);
		}
	}
	
	/**
	 * Returns an List of Details for a Header Id. 
	 * The argument must be a Long Object with the value of the header Id
	 *
	 * @param  		value of the header id
	 * @return      a list with the details for the passed argument (header Id)
	 */
	@SuppressWarnings("unchecked")
	public List<PaymentOrderDetBean> getPaymentOrderDetByHeadId(Long headId) throws CommonException{
		List<PaymentOrderDetBean> queryForList;
		try {
			
			//[[ queryForList is an implemented interface of iBatis]] 
			
			queryForList = (List<PaymentOrderDetBean>)this.getConfig().queryForList("paymentOrder.getPaymentOrderDetByHeadId", headId);
			return queryForList;
			
		} catch (SQLException e) {
			throw new CommonException(e);
		}
	}
	
	/**
	 * Returns Pending purchases. 
	 * The passed arguments are:
	 * <p>
	 * - A Long object containing the value of the supplier id
	 * - A Date object containing the purchase date
	 * 
	 * @param  		the supplier id
	 * @param  		the payment Order Date
	 * 
	 */
	@SuppressWarnings("unchecked")
	public List<PaymentOrderHeadBean> getPendingPaymentOrders(Long idSupplier, Date voucherDate, String status) throws CommonException, SQLException{
		List<PaymentOrderHeadBean> queryForList;
		try {
			HashMap< String, Object> param = new HashMap<String, Object>();
			param.put("idSupplier", idSupplier);
			param.put("voucherDate", voucherDate);
			param.put("status", status);
			queryForList = (List<PaymentOrderHeadBean>)this.getConfig().queryForList("paymentOrder.getPendingPaymentOrders", param);
			return queryForList;
		} catch (SQLException e) {
			this.getConfig().endTransaction();
			throw new CommonException(e);
		}
	}
	
	/**
	 * Returns Pending purchases. 
	 * The passed arguments are:
	 * <p>
	 * - A Long object containing the value of the supplier id
	 * - A Date object containing the purchase date
	 * 
	 * @param  		the supplier id
	 * @param  		the purchase Date
	 * 
	 */
	@SuppressWarnings("unchecked")
	public List<PaymentOrderHeadBean> getPaymentOrders(Long idSupplier, Date startDate, Date endDate, String status) throws CommonException, SQLException{
		List<PaymentOrderHeadBean> queryForList;
		try {
			HashMap< String, Object> param = new HashMap<String, Object>();
			param.put("idSupplier", idSupplier);
			param.put("startDate", startDate);
			param.put("endDate", endDate);
			param.put("status", status);
			queryForList = (List<PaymentOrderHeadBean>)this.getConfig().queryForList("paymentOrder.getPaymentOrders", param);
			return queryForList;
		} catch (SQLException e) {
			this.getConfig().endTransaction();
			throw new CommonException(e);
		}
	}
	
	
	/**
	 * Returns an specific header object. 
	 * The argument must be a Long Object with the value of the header Id
	 *
	 * @param  		value of the header id
	 * @return      the header object for the passed argument (id)
	 */
	public PaymentOrderHeadBean getPaymentOrderHeadById(Long id) throws CommonException{
		PaymentOrderHeadBean queryForObject;
		try {
			
			//[[ queryForObject is an implemented interface of iBatis ]] 
			
			queryForObject = (PaymentOrderHeadBean)this.getConfig().queryForObject("paymentOrder.getPaymentOrderHeadById", id);
			return queryForObject;
			
		} catch (SQLException e) {
			throw new CommonException(e);
		}
	}
	
	
	public PaymentOrderHeadBean getPaymentOrderHeadByEntVoucher(Long pEnt, Long pVoucher) throws CommonException{
		PaymentOrderHeadBean queryForObject;
		try {
			//[[ queryForObject is an implemented interface of iBatis ]]
			HashMap< String, Object> param = new HashMap<String, Object>();
			param.put("pEnt", pEnt);
			param.put("pVoucher", pVoucher);
			queryForObject = (PaymentOrderHeadBean)this.getConfig().queryForObject("paymentOrder.getPaymentOrderHeadByEntVoucher", param);
			return queryForObject;
			
		} catch (SQLException e) {
			throw new CommonException(e);
		}
	}
	
	
	
	/**
	 * Insert a Header Record. 
	 * The passed argument is:
	 * <p>
	 * - A entity Object containing the header Fields
	 * 
	 * @param  		the header entity bean(PaymentOrderHeadBean)
	 */
	public void saveNewPaymentOrderHead(PaymentOrderHeadBean paymentOrderHead) throws CommonException{
		try {
			this.getConfig().startTransaction();
			this.getConfig().insert("paymentOrder.insPaymentOrderHead", paymentOrderHead);
			this.getConfig().commitTransaction();
		} catch (SQLException e) {
			e.printStackTrace();
			throw new CommonException(e);
		} finally {
			try {
				this.getConfig().endTransaction();
			} catch (SQLException e) {
				e.printStackTrace();
				throw new CommonException(e);
			}
		}
	}
	
	
	public void saveNewPaymentOrderDet(PaymentOrderDetBean paymentOrderDet) throws CommonException{
		try {
			this.getConfig().startTransaction();
			this.getConfig().insert("paymentOrder.insPaymentOrderDet", paymentOrderDet);
			this.getConfig().commitTransaction();
		} catch (SQLException e) {
			e.printStackTrace();
			throw new CommonException(e);
		} finally {
			try {
				this.getConfig().endTransaction();
			} catch (SQLException e) {
				e.printStackTrace();
				throw new CommonException(e);
			}
		}
	}
	
	/**
	 * Confirm a specific order. 
	 * The passed argument is:
	 * <p>
	 * - A Long object with the order id
	 * 
	 * @param  		the order id
	 */
	public void confirmPaymentOrder(Long idPaymentOrder) throws CommonException{
		try {
			this.getConfig().startTransaction();
			this.getConfig().update("paymentOrder.confirmPaymentOrderById", idPaymentOrder);
			this.getConfig().commitTransaction();
		} catch (SQLException e) {
			throw new CommonException(e);
		} finally {
			try {
				this.getConfig().endTransaction();
			} catch (SQLException e) {
				e.printStackTrace();
				throw new CommonException(e);
			}
		}
	}
	
	/**
	 * Cancel a specific order. 
	 * The passed argument is:
	 * <p>
	 * - A Long object with the order id
	 * 
	 * @param  		the order id
	 */
	public void cancelPaymentOrder(Long idPaymentOrder) throws CommonException{
		try {
			this.getConfig().startTransaction();
			this.getConfig().update("paymentOrder.cancelPaymentOrderById", idPaymentOrder);
			this.getConfig().commitTransaction();
		} catch (SQLException e) {
			throw new CommonException(e);
		} finally {
			try {
				this.getConfig().endTransaction();
			} catch (SQLException e) {
				e.printStackTrace();
				throw new CommonException(e);
			}
		}
	}
	
	
//	/**
//	 * Insert Detail Record. 
//	 * The passed argument is:
//	 * <p>
//	 * - A entity Object containing the detail fields including the header id
//	 * 
//	 * @param  		the detail entity bean(PaymentOrderDetBean)
//	 */
//	public void saveNewPaymentOrderDet(PaymentOrderDetBean purchasesDet) throws CommonException{
//		try {
//			this.getConfig().startTransaction();
//			this.getConfig().insert("paymentOrder.insPaymentOrdersDet", purchasesDet);
//			this.getConfig().commitTransaction();
//		} catch (SQLException e) {
//			e.printStackTrace();
//			throw new CommonException(e);
//		} finally {
//			try {
//				this.getConfig().endTransaction();
//			} catch (SQLException e) {
//				e.printStackTrace();
//				throw new CommonException(e);
//			}
//		}
//	}
//	
//	
//	/**
//	 * Update a Header Record. 
//	 * The passed argument is:
//	 * <p>
//	 * - A entity Object containing the header Fields
//	 * 
//	 * @param  		the header entity bean(PaymentOrderHeadBean)
//	 */
//	public void updatePaymentOrderHead(PaymentOrderHeadBean purchasesHead) throws CommonException{
//		try {
//			this.getConfig().startTransaction();
//			this.getConfig().update("paymentOrder.udpPaymentOrdersHeadById", purchasesHead);
//			this.getConfig().commitTransaction();
//		} catch (SQLException e) {
//			throw new CommonException(e);
//		} finally {
//			try {
//				this.getConfig().endTransaction();
//			} catch (SQLException e) {
//				e.printStackTrace();
//				throw new CommonException(e);
//			}
//		}
//	}
	
	
//	/**
//	 * update a Detail Record. 
//	 * The passed argument is:
//	 * <p>
//	 * - A entity Object containing the detail fields including the header id
//	 * 
//	 * @param  		the detail entity bean(PaymentOrderDetBean)
//	 */
//	public void updatePaymentOrderDet(PaymentOrderDetBean purchasesDet) throws CommonException{
//		try {
//			this.getConfig().startTransaction();
//			this.getConfig().update("paymentOrder.udpPaymentOrdersDetById", purchasesDet);
//			this.getConfig().commitTransaction();
//		} catch (SQLException e) {
//			throw new CommonException(e);
//		} finally {
//			try {
//				this.getConfig().endTransaction();
//			} catch (SQLException e) {
//				e.printStackTrace();
//				throw new CommonException(e);
//			}
//		}
//	}
	
	
//	/**
//	 * Confirm a specific PaymentOrder. 
//	 * The passed argument is:
//	 * <p>
//	 * - A Long object with the PaymentOrder id
//	 * 
//	 * @param  		the PaymentOrder id
//	 */
//	public void confirmPaymentOrder(Long idPaymentOrder) throws CommonException{
//		try {
//			this.getConfig().startTransaction();
//			/*cambiar de estado a la compra*/
//			this.getConfig().update("paymentOrder.confirmPaymentOrderById", idPaymentOrder);
//			this.getConfig().commitTransaction();
//		} catch (SQLException e) {
//			throw new CommonException(e);
//		} finally {
//			try {
//				this.getConfig().endTransaction();
//			} catch (SQLException e) {
//				e.printStackTrace();
//				throw new CommonException(e);
//			}
//		}
//	}
	
	/**
	 * Cancel a specific PaymentOrder. 
	 * The passed argument is:
	 * <p>
	 * - A Long object with the PaymentOrder id
	 * 
	 * @param  		the PaymentOrder id
	 */
//	public void cancelPaymentOrder(Long idPaymentOrder) throws CommonException{
//		try {
//			this.getConfig().startTransaction();
//			this.getConfig().update("paymentOrder.cancelPaymentOrderById", idPaymentOrder);
//			this.getConfig().commitTransaction();
//		} catch (SQLException e) {
//			throw new CommonException(e);
//		} finally {
//			try {
//				this.getConfig().endTransaction();
//			} catch (SQLException e) {
//				e.printStackTrace();
//				throw new CommonException(e);
//			}
//		}
//	}

}
